package com.mall.common;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
// 保证序列化json的时候, 如果是null的对象, key也会消失
public class ServiceRespones<T> implements Serializable {
    private int status;
    private String msg;
    private T data;

    private ServiceRespones(int status) {
        this.status = status;
    }
    private ServiceRespones(String msg) {
        this.msg = msg;
    }
    private ServiceRespones(int status, T data) {
        this.status = status;
        this.data = data;
    }
    private ServiceRespones(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
    private ServiceRespones(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }


    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    @JsonIgnore
    // 使之不在json序列化中
    public boolean isSuccess() {
        return this.status == ResponeCode.SUCCESS.getCode();
    }

    /** 成功 */
    public static <T> ServiceRespones<T> createBySuccess() {
        return new ServiceRespones<T>(ResponeCode.SUCCESS.getCode());
    }
    public static <T> ServiceRespones<T> createBySuccessMessage(String msg) {
        return new ServiceRespones<T>(ResponeCode.SUCCESS.getCode(), msg);
    }
    public static <T> ServiceRespones<T> createBySuccess(T data) {
        return new ServiceRespones<T>(ResponeCode.SUCCESS.getCode(), data);
    }
    public static <T> ServiceRespones<T> createBySuccess(String msg, T data) {
        return new ServiceRespones<T>(ResponeCode.SUCCESS.getCode(), msg, data);
    }

    /** 失败 */
    public static <T> ServiceRespones<T> createByError() {
        return new ServiceRespones<T>(ResponeCode.ERROR.getCode(), ResponeCode.ERROR.getDesc());
    }
    public static <T> ServiceRespones<T> createByErrorMessage(String errorMsg) {
        return new ServiceRespones<T>(ResponeCode.ERROR.getCode(), errorMsg);
    }
    public static <T> ServiceRespones<T> createByError(int errorCode, String errorMsg) {
        return new ServiceRespones<T>(errorCode, errorMsg);
    }

}
